package cn.techwis.util;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.*;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

import java.io.InputStream;


/**
 * 微信 请求工具类
 * get post
 */
public class WxHttpUtil {

    private static final Log log = LogFactory.get();

    public static final JSONObject requestPost(String requestMsg, String url, Object params, String privateKeyFileName, String mchId, String serial_no) {
        String toJsonStr = JSONUtil.toJsonStr(params);
        String authorization = null;
        try {
            authorization = PayOrderSignUtil.getAuthorization(Method.POST.name(), toJsonStr, url, privateKeyFileName, mchId, serial_no);
        } catch (Exception e) {
            e.printStackTrace();
        }

        log.info("{}=>请求地址:{}", requestMsg, url);
        log.info("{}=>请求参数:{}", requestMsg, toJsonStr);
        log.info("{}=>Authorization:{}", requestMsg, authorization);
        HttpRequest post = HttpUtil.createPost(url);
        post.contentType(ContentType.JSON.getValue());
        post.auth(authorization);
        post.header(Header.ACCEPT, ContentType.JSON.getValue());
        post.body(toJsonStr);
        HttpResponse execute = post.execute();

        String body = execute.body();
        JSONObject object = new JSONObject();
        if (StrUtil.isNotEmpty(body)) {
            object = JSONUtil.parseObj(body);
        }
        int status = execute.getStatus();
        //组装返回状态码进去
        object.putOpt("reponseStatus", status);
        log.info("{}=>返回参数:{}", requestMsg, object.toString());
        if (!object.isEmpty()) {
            return object;
        } else {
            log.error("{}=>请求失败:{}", "返回参数为空");
        }
        throw ExceptionUtil.wrapRuntime("请求失败,未知错误");
    }

    public static final JSONObject requestGet(String requestMsg, String url, String privateKeyFileName, String mchId, String serial_no) {
        String authorization = null;
        try {
            authorization = PayOrderSignUtil.getAuthorization(Method.GET.name(), null, url, privateKeyFileName, mchId, serial_no);
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("{}=>请求地址:{}", requestMsg, url);
        log.info("{}=>Authorization:{}", requestMsg, authorization);
        HttpRequest get = HttpUtil.createGet(url);
        get.auth(authorization);
        HttpResponse execute = get.execute();

        String body = execute.body();
        JSONObject object = new JSONObject();
        if (StrUtil.isNotEmpty(body)) {
            object = JSONUtil.parseObj(body);
        }
        int status = execute.getStatus();
        //组装返回状态码进去
        object.putOpt("reponseStatus", status);
        log.info("{}=>返回参数:{}", requestMsg, object.toString());
        if (!object.isEmpty()) {
            return object;
        } else {
            log.error("{}=>请求失败:{}", "返回参数为空");
        }
        throw ExceptionUtil.wrapRuntime("请求失败,未知错误");
    }


    /**
     * 下载证书 get请求
     * @param requestMsg
     * @param url
     * @param privateKeyFileName
     * @param mchId
     * @param serial_no
     * @return
     */
    public static final InputStream requestGetDownBill(String requestMsg, String url, String privateKeyFileName, String mchId, String serial_no) {
        String authorization = null;
        try {
            authorization = PayOrderSignUtil.getAuthorization(Method.GET.name(), null, url, privateKeyFileName, mchId, serial_no);
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("{}=>请求地址:{}", requestMsg, url);
        log.info("{}=>Authorization:{}", requestMsg, authorization);
        HttpRequest get = HttpUtil.createGet(url);
        get.auth(authorization);
        HttpResponse execute = get.execute();
        InputStream inputStream = execute.bodyStream();
        if(inputStream==null){
            String body = execute.body();
            JSONObject object = new JSONObject();
            if (StrUtil.isNotEmpty(body)) {
                object = JSONUtil.parseObj(body);
            }
            int status = execute.getStatus();
            //组装返回状态码进去
            object.putOpt("reponseStatus", status);
            log.info("{}=>返回参数:{}", requestMsg, object.toString());
            if (!object.isEmpty()) {
                throw ExceptionUtil.wrapRuntime(object.toString());
            } else {
                log.error("{}=>请求失败:{}", "返回参数为空");
            }
            throw ExceptionUtil.wrapRuntime("请求失败,未知错误");
        }
        return inputStream;

    }

}
